
//调用方法request ，，无loading加载框提示
function request(url, params, success, fail) {
    this.requestLoading(url, params, "", success, fail)
}

//调用方法requestLoading ，，会出现loading加载框提示
// 网络请求
// url:网络请求的url
// params:请求参数
// message:进度提示信息
// success:成功的回调函数
// fail：失败的回调
function requestLoading(url, params, message, success, fail) { 

    wx.getNetworkType({
        success: function (res) {
            // 返回值
            var networkType = res.networkType
            if (networkType === "none"){
                wx.showLoading({
                    title: "网络连接失败"
                })
                setTimeout(function () {
                    wx.hideLoading()
                }, 2000)
            }else{
                wx.showNavigationBarLoading()
                if (message != "") {
                    wx.showLoading({
                        title: message,
                    })
                }
                wx.request({
                    url: url,
                    data: params,
                    header: {
                        //'Content-Type': 'application/json'
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    method: 'get',
                    success: function (res) {
                        console.log(res.data)
                        wx.hideNavigationBarLoading()
                        if (message != "") {
                            wx.hideLoading()
                        }
                        if (res.statusCode == 200) {
                            success(res.data)
                        } else {
                            wx.showLoading({
                                title: "请求失败" + res.errMsg
                            })
                            setTimeout(function () {
                                wx.hideLoading()
                            }, 2000)
                            fail()
                        }
                    },
                    fail: function (res) {
                        wx.hideNavigationBarLoading()
                        if (message != "") {
                            wx.hideLoading()
                        }
                        wx.showLoading({
                            title: "请求失败" + res.errMsg
                        })
                        setTimeout(function () {
                            wx.hideLoading()
                        }, 2000)
                        fail()
                    },
                    complete: function (res) {

                    },
                })
            }
        }
    })


    
}
module.exports = {
    request: request,
    requestLoading: requestLoading, 
    stop:false //暂停/打开首页动画切换，，false 打开 true停止
}
